package com.example.bysj.Dao;
//201902104061徐中堂
import com.example.bysj.domain.School;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;


import java.util.List;
//声明数据库访问类
@Repository("schoolDao")
public class SchoolDao {
    //自动注入
    @Autowired
    private JdbcTemplate jdbcTemplate;
    //增加学院
    public boolean add(School school) {
        String sql = "INSERT INTO school (no,description,remarks) VALUES (?,?,?)";
        int affectedRowNum = jdbcTemplate.update(sql, school.getNo(), school.getDescription(),school.getRemarks());
        if (affectedRowNum != 1) {
            return false;
        } else {

            return true;
        }
    }
    //根据id查找学院
    public School find(int id){
        String sql = "SELECT * FROM school WHERE id = ?";
        List<School> schools = jdbcTemplate.query(sql, new Object[]{id},
                new BeanPropertyRowMapper<>(School.class));
        if(!schools.isEmpty()){
            return schools.get(0);
        } else
            return null;
    }
    //查找全部的学院
    public List<School> findAll(){
        String sql = "SELECT * FROM school";
        List<School> schools = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(School.class){

        });
        return schools;
    }
    //修改学院
    public boolean update(School school){
        String sql = "UPDATE school SET no = ? ,description=? WHERE id = ?";
        int affectedRowNum = jdbcTemplate.update(sql,school.getNo(),school.getDescription(),school.getId());
        if (affectedRowNum!=1){
            return false;
        }else{
            return true;
        }
    }
    //根据id查找学院
    public boolean delete(int id){
        String sql = "DELETE FROM school WHERE id = ? ";
        int affectedRowNum = jdbcTemplate.update(sql,id);
        if (affectedRowNum!=1){
            return false;
        }else{
            return true;
        }
    }
}
